package com.wdc.controller.user;


import com.wdc.context.BaseContext;
import com.wdc.model.domain.User;
import com.wdc.model.request.LoginRequest;
import com.wdc.model.request.RegisterRequest;
import com.wdc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Autowired
    private UserService userService;


    /**
     * 注册
     * @param registerRequest 请求参数
     */
    @PostMapping("/register")
    public int register(@RequestBody RegisterRequest registerRequest){
        if (registerRequest == null){
            return -1;
        }
        log.info("Register, {}",registerRequest);
        String username = registerRequest.getUserAccount();
        String password = registerRequest.getPassword();
        String ensurePassword = registerRequest.getEnsurePassword();
        int i = userService.register(username, password, ensurePassword);

         return i;

    }



    @PostMapping("/login")
    public User Login(@RequestBody LoginRequest loginRequest, HttpServletRequest request){
        if (loginRequest == null){
            return null;
        }
        log.info("Login, {}",loginRequest);
        User user = userService.login(loginRequest,request);

        return user;


    }



}
